﻿using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Messaging;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using ZFSDomain.Common.CoreLib;
using ZFSDomain.Interface.Base;

namespace ZFSDomain.SysModule
{
    /// <summary>
    /// 主窗口基类
    /// </summary>
    public abstract partial class BaseOperation<T> : ViewModelBase where T : class, new()
    {
        #region 属性

        private string searchText = string.Empty;
        private ObservableCollection<ToolBarDefault<T>> buttonDefaults;

        /// <summary>
        /// 搜索内容
        /// </summary>
        public string SearchText
        {
            get { return searchText; }
            set { searchText = value; RaisePropertyChanged(); }
        }

        private ObservableCollection<T> _GridModelList;

        /// <summary>
        /// 表单数据
        /// </summary>
        public ObservableCollection<T> GridModelList
        {
            get { return _GridModelList; }
            set { _GridModelList = value; RaisePropertyChanged(); }
        }


        /// <summary>
        /// 功能集合
        /// </summary>
        public ObservableCollection<ToolBarDefault<T>> ButtonDefaults
        {
            get { return buttonDefaults; }
            set { buttonDefaults = value; RaisePropertyChanged(); }
        }

        #endregion

        #region 默认实现

        /// <summary>
        /// 初始化
        /// </summary>
        public virtual void InitViewModel()
        {
            GridModelList = new ObservableCollection<T>();
            this.SetDefaultButton();
            this.SetButtonAuth();
            this.GetPageData(this.PageIndex);
        }

        /// <summary>
        /// 设置默认按钮
        /// </summary>
        public virtual void SetDefaultButton()
        {
            ButtonDefaults = new ObservableCollection<ToolBarDefault<T>>();
            ButtonDefaults.Add(new ToolBarDefault<T>() { AuthValue = Authority.ADD, ModuleName = "新增", Command = this.AddCommand });
            ButtonDefaults.Add(new ToolBarDefault<T>() { AuthValue = Authority.EDIT, ModuleName = "编辑", Command = this.EditCommand, IsVisibility = true, Hide = true });
            ButtonDefaults.Add(new ToolBarDefault<T>() { AuthValue = Authority.DELETE, ModuleName = "删除", Command = this.DelCommand, IsVisibility = true, Hide = true });
        }

        #endregion

        #region 功能命令

        private RelayCommand<T> _addCommand;
        private RelayCommand<T> _editCommand;
        private RelayCommand<T> _delCommand;
        private RelayCommand _queryCommand;
        private RelayCommand _resetCommand;

        /// <summary>
        /// 新增
        /// </summary>
        public RelayCommand<T> AddCommand
        {
            get
            {
                if (_addCommand == null)
                {
                    _addCommand = new RelayCommand<T>(t => Add(t));
                }
                return _addCommand;
            }
            set { _addCommand = value; }
        }

        /// <summary>
        /// 编辑
        /// </summary>
        public RelayCommand<T> EditCommand
        {
            get
            {
                if (_editCommand == null)
                {
                    _editCommand = new RelayCommand<T>(t => Edit(t));
                }
                return _editCommand;
            }
            set { _editCommand = value; }
        }

        /// <summary>
        /// 删除
        /// </summary>
        public RelayCommand<T> DelCommand
        {
            get
            {
                if (_delCommand == null)
                {
                    _delCommand = new RelayCommand<T>(t => Del(t));
                }
                return _delCommand;
            }
            set { _delCommand = value; }
        }

        /// <summary>
        /// 查询
        /// </summary>
        public RelayCommand QueryCommand
        {
            get
            {
                if (_queryCommand == null)
                {
                    _queryCommand = new RelayCommand(() => Query());
                }
                return _queryCommand;
            }
            set { _queryCommand = value; }
        }

        /// <summary>
        /// 重置
        /// </summary>
        public RelayCommand ResetCommand
        {
            get
            {
                if (_resetCommand == null)
                {
                    _resetCommand = new RelayCommand(() => Reset());
                }
                return _resetCommand;
            }
            set { _resetCommand = value; }
        }



        #endregion
    }

    /// <summary>
    /// 主窗口/分布基类
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public abstract partial class BaseOperation<T> : IDataOperation, IPermission, IDataPager
    {
        #region IPermission

        protected int? authValue;

        /// <summary>
        /// 权限值
        /// </summary>
        public int? AuthValue { get { return authValue; } set { authValue = value; } }


        /// <summary>
        /// 验证按钮权限
        /// </summary>
        /// <param name="authValue"></param>
        /// <returns></returns>
        public bool GetButtonAuth(int authValue)
        {
            var def = ButtonDefaults.FirstOrDefault(t => (authValue & t.AuthValue) == t.AuthValue
            && (t.IsVisibility.Equals(false)
            || t.Hide.Equals(true)));

            if (def != null)
                return true;
            else
                return false;
        }

        /// <summary>
        /// 设置权限
        /// </summary>
        public void SetButtonAuth()
        {
            if (Loginer.LoginerUser.IsAdmin) return;

            foreach (var b in ButtonDefaults)
                if ((this.AuthValue & b.AuthValue) != b.AuthValue)
                    b.IsVisibility = true; //隐藏功能
        }


        #endregion

        #region IDataPager

        public RelayCommand GoHomePageCommand { get { return new RelayCommand(() => GoHomePage()); } }

        public RelayCommand GoOnPageCommand { get { return new RelayCommand(() => GoOnPage()); } }

        public RelayCommand GoNextPageCommand { get { return new RelayCommand(() => GoNextPage()); } }

        public RelayCommand GoEndPageCommand { get { return new RelayCommand(() => GoEndPage()); } }


        private int totalCount = 0;
        private int pageSize = 15;
        private int pageIndex = 1;
        private int pageCount = 0;

        /// <summary>
        /// 总数
        /// </summary>
        public int TotalCount { get { return totalCount; } set { totalCount = value; RaisePropertyChanged(); } }

        /// <summary>
        /// 当前页大小
        /// </summary>
        public int PageSize { get { return pageSize; } set { pageSize = value; RaisePropertyChanged(); } }

        /// <summary>
        /// 当前页
        /// </summary>
        public int PageIndex { get { return pageIndex; } set { pageIndex = value; RaisePropertyChanged(); } }

        /// <summary>
        /// 分页总数
        /// </summary>
        public int PageCount { get { return pageCount; } set { pageCount = value; RaisePropertyChanged(); } }

        /// <summary>
        /// 首页
        /// </summary>
        public virtual void GoHomePage()
        {
            if (this.PageIndex == 1) return;

            PageIndex = 1;

            GetPageData(PageIndex);
        }

        /// <summary>
        /// 上一页
        /// </summary>
        public virtual void GoOnPage()
        {
            if (this.PageIndex == 1) return;

            PageIndex--;

            this.GetPageData(PageIndex);
        }

        /// <summary>
        /// 下一页
        /// </summary>
        public virtual void GoNextPage()
        {
            if (this.PageIndex == PageCount) return;

            PageIndex++;

            this.GetPageData(PageIndex);
        }

        /// <summary>
        /// 尾页
        /// </summary>
        public virtual void GoEndPage()
        {
            this.PageIndex = PageCount;

            GetPageData(PageCount);
        }

        /// <summary>
        /// 获取数据
        /// </summary>
        /// <param name="pageIndex"></param>
        public  abstract void GetPageData(int pageIndex);

        /// <summary>
        /// 设置页数
        /// </summary>
        public virtual void SetPageCount()
        {
            PageCount = Convert.ToInt32(Math.Ceiling((double)TotalCount / (double)PageSize));
        }

        #endregion

        #region IDataOperation

        /// <summary>
        /// 新增
        /// </summary>
        public abstract void Add<TModel>(TModel model);

        /// <summary>
        /// 编辑
        /// </summary>
        public abstract void Edit<TModel>(TModel model);

        /// <summary>
        /// 删除
        /// </summary>
        public abstract void Del<TModel>(TModel model);

        /// <summary>
        /// 查询
        /// </summary>
        public virtual  void Query()
        {
            this.GetPageData(this.PageIndex);
        }

        /// <summary>
        /// 重置
        /// </summary>
        public virtual void Reset()
        {
            this.SearchText = string.Empty;
        }

        #endregion
    }

    /// <summary>
    /// 弹出式窗口基类
    /// </summary>
    public class BaseDialogOperation : ViewModelBase
    {
        /// <summary>
        /// 结果，用户点击确定Result=true;
        /// </summary>
        public bool Result { get; set; }

        private RelayCommand _saveCommand;
        private RelayCommand _cancelCommand;

        /// <summary>
        /// 保存
        /// </summary>
        public RelayCommand SaveCommand
        {
            get
            {
                if (_saveCommand == null)
                {
                    _saveCommand = new RelayCommand(() => Save());
                }
                return _saveCommand;
            }
            set { _saveCommand = value; }
        }

        /// <summary>
        /// 取消
        /// </summary>
        public RelayCommand CancelCommand
        {
            get
            {
                if (_cancelCommand == null)
                {
                    _cancelCommand = new RelayCommand(() => Cancel());
                }
                return _cancelCommand;
            }
            set { _cancelCommand = value; }
        }

        public virtual void Save() { this.Result = true; Messenger.Default.Send("", "CloseWindow");  }
        public virtual void Cancel() { this.Result = false; Messenger.Default.Send("", "CloseWindow");  }

    }
}
